//
//  Local_Notifations.m
//  Local_Notifacation
//
//  Created by JM Zhao on 2017/5/2.
//  Copyright © 2017年 JunMing. All rights reserved.
//

#import "Local_Notifations.h"
#import <UIKit/UIKit.h>

@implementation Local_Notifations

+ (void)addLocalNotifations:(NSString *)notiBody userInfo:(NSDictionary *)userInfo
{
    UILocalNotification *localNote = [[UILocalNotification alloc] init];
    localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:10.0];
    localNote.timeZone = [NSTimeZone defaultTimeZone];
    localNote.alertBody = notiBody;
    
    // 2.3.设置滑块的文字（锁屏状态下：滑动来“解锁”）
    localNote.alertAction = @"请解锁屏幕来启动APP";
    localNote.hasAction = NO;
    
    // 2.5.设置点击通知的启动图片
    localNote.alertLaunchImage = @"noti";
    //    localNote.repeatInterval = kCFCalendarUnitDay;
    
    // 2.6.设置alertTitle
    localNote.alertTitle = @"Photo Locker";
    localNote.soundName = UILocalNotificationDefaultSoundName;
    localNote.userInfo = userInfo;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNote];
}

+ (void)cancleLocalNotifation:(NSString *)info
{
    // 取消某一个通知
    NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];
    if (!notificaitons || notificaitons.count <= 0) {return;}
    for (UILocalNotification *notify in notificaitons) {
        
        if ([[notify.userInfo objectForKey:@"noti"] isEqualToString:info]) {
            
            [[UIApplication sharedApplication] cancelLocalNotification:notify];
            break;
        }
    }
}

+ (void)calcleAllLocalNotifation
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

+(void)loactioanNotifation:(NSString *)notiBody userInfo:(NSDictionary *)userInfo notiDate:(NSDate *)notiDate repeat:(BOOL)repeat
{
    // 设置一个按照固定时间的本地推送
    //    NSDate *now = [NSDate date];
    
    //    // 取得系统时间
    //    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    //    NSDateComponents *components = [[NSDateComponents alloc] init];
    //    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    //    components = [calendar components:unitFlags fromDate:now];
    //    NSInteger hour = [components hour];
    //    NSInteger min = [components minute];
    //    NSInteger sec = [components second];
    //    NSInteger week = [components weekday];
    //    NSLog(@"现在是%ld：%ld：%ld,周%ld",hour,min,sec,week);
    
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    
    //设置时区（跟随手机的时区）
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    
    if (localNotification) {
        
        localNotification.alertBody = notiBody;
        localNotification.alertAction = @"打开";
        localNotification.alertTitle = @"Photo Locker";
        localNotification.soundName = UILocalNotificationDefaultSoundName;
        
        //小图标数字
//        localNotification.applicationIconBadgeNumber = 1;
        localNotification.fireDate = notiDate;
    }
    
    if (repeat) {
        //循环通知的周期
        localNotification.repeatInterval = kCFCalendarUnitDay;
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
    }
    
    //设置userinfo方便撤销
    NSDictionary *info = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
    localNotification.userInfo = info;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

@end
